//
//  MRSearchViewController.m
//  Marrymemo
//
//  Created by dllo on 15/9/14.
//  Copyright (c) 2015年 LiMuRan. All rights reserved.
//

#import "MRSearchViewController.h"
#import "MRSearchCell.h"
#import "MRSearchDetailViewController.h"

@interface MRSearchViewController ()<UISearchBarDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong) UISearchBar *searchBar;

@property(nonatomic,strong) NSMutableArray *collectionArr;

@property(nonatomic,copy) NSString *searchText;

@property(nonatomic,copy) NSString *text;

@property(nonatomic,strong) UICollectionView *collectionView;

@property(nonatomic,strong) NSMutableArray *arr;

@end

@implementation MRSearchViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectionArr = [NSMutableArray arrayWithObjects:@"太平洋",@"经济",@"豪华",@"公寓",@"高级服务", nil];;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.placeLabel.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(20 * WID, 5 * HEI, 250 * WID, 30 * HEI)] ;
//    self.searchBar.backgroundColor = [UIColor colorWithRed:1.000 green:0.923 blue:0.884 alpha:1.000];
    self.searchBar.placeholder = @"搜索酒店...";
    self.searchBar.barStyle = UIBarStyleDefault;
    self.navigationItem.titleView = self.searchBar;
    self.searchBar.delegate = self;
    self.view.frame = CGRectMake(0, 64, WIDTH, HEIGHT + 64);
}

#pragma mark 当searchBarText开始编辑的时候,创建titleLabel和collectionView
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
//    [searchBar setShowsSearchResultsButton:YES];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * WID, 70 * HEI, 200 * WID, 30 * HEI)];
//    titleLabel.layer.borderWidth = 1;
//    titleLabel.layer.masksToBounds = YES;
    titleLabel.layer.borderColor = [[UIColor grayColor] CGColor];
    titleLabel.text = @"搜索热词";
    titleLabel.backgroundColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textColor = [UIColor grayColor];
    [self.view addSubview:titleLabel];
    
    [self createCollectionView];
  
}

#pragma mark 点击搜索按钮的时候,键盘失去第一响应者,并且push到下一个页面
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [searchBar setShowsCancelButton:NO animated:YES];
    
    MRSearchDetailViewController *searchDetailVC = [[MRSearchDetailViewController alloc] init];
    
    searchDetailVC.searchText = self.searchText;
    searchDetailVC.Id = self.Id;
    searchDetailVC.inLabelText = self.inLabelText;
    searchDetailVC.outLabelText = self.outLabelText;
    
    
    [self.navigationController pushViewController:searchDetailVC animated:YES];
    
    
    
}

-(void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(80 * WID, 25 * HEI);
    flowLayout.minimumInteritemSpacing = 15 * WID;
    flowLayout.minimumLineSpacing = 15 * HEI;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5 * WID, 100 * HEI, WIDTH - 5 * WID, 200 * HEI) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
//    [self.collectionView registerClass:[MRSearchCell class] forCellWithReuseIdentifier:@"search"];
    
    [self.collectionView registerClass:[MRSearchCell class] forCellWithReuseIdentifier:@"search"];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectionArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MRSearchCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"search" forIndexPath:indexPath];
    
    cell.label.text = [self.collectionArr objectAtIndex:indexPath.row];
    cell.label.textColor = [UIColor lightGrayColor];
    
    return cell;
}

#pragma mark 点击cell的时候将text赋给searchBar.text, 是下一页中POST请求的body体中的字符串
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.searchBar.text = [self.collectionArr objectAtIndex:indexPath.item];
    self.searchText = self.searchBar.text;
//    NSLog(@"hjkhl");
}

// 当文字发生变化的时候 将值赋给searchText,然后在点击方法中传给下一页
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    searchBar.text = searchText;
    NSString *string = [searchBar.text stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    self.searchText = string;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
